package com.wujian.love.service.impl;

import java.util.List;

import cn.hutool.core.convert.Convert;
import com.wujian.love.domin.Novel;
import com.wujian.love.redis.RedisKey;
import com.wujian.love.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wujian.love.mapper.NovelMapper;
import com.wujian.love.service.INovelService;

/**
 * 小说
 * 服务层实现
 *
 * @author wujian
 * @date 2021-03-04
 */
@Service
public class NovelServiceImpl implements INovelService {
    @Autowired
    private NovelMapper novelMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询小说
     * 信息
     *
     * @param id 小说
     *           ID
     * @return 小说
     * 信息
     */
    @Override
    public Novel selectNovelById(Integer id) {
        return novelMapper.selectNovelById(id);
    }

    /**
     * 查询小说
     * 列表
     *
     * @param novel 小说
     *              信息
     * @return 小说
     * 集合
     */
    @Override
    public List<Novel> selectNovelList(Novel novel) {
        return novelMapper.selectNovelList(novel);
    }

    /**
     * 新增小说
     *
     * @param novel 小说
     *              信息
     * @return 结果
     */
    @Override
    public int insertNovel(Novel novel) {
        return novelMapper.insertNovel(novel);
    }

    /**
     * 修改小说
     *
     * @param novel 小说
     *              信息
     * @return 结果
     */
    @Override
    public int updateNovel(Novel novel) {
        return novelMapper.updateNovel(novel);
    }

    /**
     * 删除小说
     * 对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNovelByIds(String ids) {
        return novelMapper.deleteNovelByIds(Convert.toStrArray(ids));
    }

    @Override
    public void cacheNovel() {
        List<Novel> novels = novelMapper.selectNovelList(null);
        for (Novel novel : novels) {
            String novelKey = RedisKey.getNovelKey(novel.getId());
            redisService.set(novelKey,novel);
        }


    }

}
